package org.cc.cloud;

import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerEndpoint;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//可以消费的第二个类
@Configuration
public class RabbitListenerConfig {
	@Bean
	public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
		SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
		factory.setConnectionFactory(connectionFactory);
		return factory;
	}
	
	@Bean
	public RabbitListenerConfigurer rabbitListenerConfigurer() {
		return new RabbitListenerConfigurer() {

			@Override
			public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
				// TODO Auto-generated method stub
				SimpleRabbitListenerEndpoint endpoint = new SimpleRabbitListenerEndpoint();
				endpoint.setId("10");
				endpoint.setQueueNames("weixin");
				endpoint.setMessageListener(msg -> {
					System.out.println("+++++++++SimpleRabbitListenerEndpoint++++++++++");
					System.out.println(new String(msg.getBody()));
				});
				registrar.registerEndpoint(endpoint);
				//同样可以使用适配器的方式
				//MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageHandler());
				//container.setMessageListener(adapter);
				//具体请看RabbitMqConfig类
			}
			
		};
	};
}
